/**
 * 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
 * 日    期：2020-12-15
 */
package com.rzico.article.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.article.entity.BillChefAffiliated;
import com.rzico.article.model.ShopBillFare;
import com.rzico.article.service.BillChefAffiliatedService;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
/**
 * 名厨关联店铺控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-12-15
 */
@Api(description = "名厨关联店铺接口")
@RestController
@RequestMapping("/admin/billFareAffiliated")
public class BillChefAffiliatedAdminController extends BaseController {

    @Autowired
    private BillChefAffiliatedService billChefAffiliatedService;

    /**
     * 分页查询名厨关联店铺
     *
     * @return
     */
    @ApiOperation("分页查询名厨关联店铺")
    @GetMapping("/chef/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "chefId", value = "名厨ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "shopName", value = "店铺名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型{0:商品,1:名店,2:酒店,3:名厨,4.餐车}", dataType = "String", paramType = "query")
    })
    public CommResult<ShopBillFare> list(Long chefId,String shopName,Integer type, Pageable pageable) {

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ShopBillFare> list = billChefAffiliatedService.selectBillChefList(chefId,shopName,type);
        PageResult<ShopBillFare> pageResult = new PageResult<ShopBillFare>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 分页查询名厨信息
     *
     * @return
     */
    @ApiOperation("查询名厨信息")
    @GetMapping("/chef/find")
    @ApiImplicitParams({@ApiImplicitParam(name = "chefId", value = "名厨ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型{0:商品,1:名店,2:酒店,3:名厨,4.餐车}", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "typeId", value = "类型id", dataType = "String", paramType = "query")
    })
    public CommResult<BillChefAffiliated> find(Long chefId,Integer type,Long typeId,Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (chefId!=null){
            params.put("chefId", chefId);
        }
        if (chefId!=null){
            params.put("type", type);
        }
        if (typeId!=null){
            params.put("typeId", typeId);
        }
        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<BillChefAffiliated> list = billChefAffiliatedService.selectFindList(params);
        PageResult<BillChefAffiliated> pageResult = new PageResult<BillChefAffiliated>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 保存名厨关联店铺
     *
     * @param billChefAffiliated
     * @return
     */
    @ApiOperation("保存名厨关联店铺")
    @PostMapping("/chef/save")
    public CommResult<BillChefAffiliated> save(@RequestBody BillChefAffiliated billChefAffiliated) {

        Map<String,Object> params = new HashMap<>();
        params.put("chefId",billChefAffiliated.getChefId());
        params.put("typeId",billChefAffiliated.getTypeId());
        Integer w = billChefAffiliatedService.selectRowCount(params);
        if (w>0) {
            return CommResult.error("不能重覆添加");
        }
        Integer type=billChefAffiliatedService.selectType(params);
        billChefAffiliated.setType(type);
        int affectCount = billChefAffiliatedService.insertUseGeneratedKeys(billChefAffiliated);
        if (affectCount <= 0) {
            return CommResult.error();
        }

        ShopBillFare shopBillFare = billChefAffiliatedService.selectBillChef(billChefAffiliated.getChefId(),billChefAffiliated.getTypeId());
        return CommResult.success(shopBillFare);

    }


    /**
     * 批量删除名厨关联店铺
     *

     * @return
     */
    @Log(desc = "批量删除名厨关联店铺", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除名厨关联店铺,ids用逗号拼接")
    @PostMapping("/chef/del")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "chefId", value = "名厨ID", dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "typeId", value = "类型ID", dataType = "Long", paramType = "query")
            })
    public CommResult<BillChefAffiliated> del(Long chefId,Long typeId) {
        BillChefAffiliated billChefAffiliated = new BillChefAffiliated();
        billChefAffiliated.setTypeId(typeId);
        billChefAffiliated.setChefId(chefId);
        billChefAffiliatedService.delete(billChefAffiliated);
        return CommResult.success();
    }

    /**
     * 更新名厨关联店铺
     *
     * @param billChefAffiliated
     * @return
     */
    @Log(desc = "更新名厨关联信息", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新名厨关联信息")
    @PostMapping("/chef/update")
    public CommResult<BillChefAffiliated> update(@RequestBody BillChefAffiliated billChefAffiliated) {
        Map<String,Object> params = new HashMap<>();
        params.put("typeId",billChefAffiliated.getTypeId());
        Integer type=billChefAffiliatedService.selectType(params);
        billChefAffiliated.setType(type);
        int affectCount = billChefAffiliatedService.updateByPrimaryKeySelective(billChefAffiliated);
        if (affectCount <= 0){
            return CommResult.error();
        }
        BillChefAffiliated result = billChefAffiliatedService.findById(billChefAffiliated.getId());
        return CommResult.success(result);
    }

}
